/**
 * 
 */
package com.aspire.android.heliar.activities;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.aspire.android.heliar.R;
import com.aspire.android.heliar.pub.Pub;
import com.aspire.android.heliar.pub.PubManager;
import com.aspire.android.heliar.pub.render.OutlineFrameRender;
import com.aspire.android.heliar.utils.ContextFactory;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * @author panglei
 * 
 */
public class PubOneFrameActivity extends Activity {

	public static final int WINDOWS_MASS_HEIGHT = 100;

	public static final int MENU_DELETE_CURRNET_PUB = 1;
	public static final int MENU_OUTLINE = 2;

	public static final int MENU_OUTLINE_START_ID = 1024;

	private int position;
	private Pub pub;
	private ScrollView scrollView;
	private OutlineFrameRender render;
	private Map<String, Integer> outlines;
	private List<Integer> outlinesOffset = new LinkedList<Integer>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.pub_content_outlines_render);
		scrollView = (ScrollView) findViewById(R.id.pubContentRenderScroll);

		Intent intent = this.getIntent();
		position = intent.getIntExtra(
				PubListActivity.INTENT_EXTRA_PUB_POSITION, 0);
		pub = PubManager.getInstance().getPub(position);

		LinearLayout rootView = (LinearLayout) findViewById(R.id.pubContentRenderScrollRootView);
		render = new OutlineFrameRender(rootView);
		render.render(pub);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		int order = 0;
		menu.add(0, MENU_DELETE_CURRNET_PUB, order,
				R.string.pub_one_frame_activity_menu_delete);
		Menu outlinesMenu = menu.addSubMenu(0, MENU_OUTLINE, order + 1,
				R.string.pub_one_frame_activity_menu_outline);

		if (outlines == null) {
			outlines = render.getOutLines();
		}

		int i = 0;
		for (String title : outlines.keySet()) {
			if (title != null && !title.equals("")) {
				this.outlinesOffset.add(outlines.get(title));
				outlinesMenu
						.add(Menu.NONE, MENU_OUTLINE_START_ID + i, 5, title);
				i++;
			}
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_DELETE_CURRNET_PUB:
			showDeletePubConfirm();
			break;
		}

		if (item.getItemId() >= MENU_OUTLINE_START_ID) {
			this.scrollView.smoothScrollTo(0, outlinesOffset.get(item
					.getItemId() - MENU_OUTLINE_START_ID));
		}

		return false;
	}

	public void showDeletePubConfirm() {
		final Dialog dialog = new Dialog(ContextFactory.getInstance());

		dialog.setContentView(R.layout.dialog_confirm_delete_pub);
		dialog.setTitle(R.string.dialog_delete_pub_title);

		Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
		Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
		final CheckBox chkSyncDeleteInbox = (CheckBox) dialog
				.findViewById(R.id.chkSyncDeleteInbox);

		buttonYes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View button) {
				PubManager.getInstance().deletePub(pub,
						chkSyncDeleteInbox.isChecked());
				dialog.cancel();
				PubOneFrameActivity.this.finish();
			}
		});

		buttonNo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View button) {
				dialog.cancel();
			}

		});

		dialog.show();

	}

	public void init() {
		ContextFactory.init(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		ContextFactory.init(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			scrollView.pageScroll(ScrollView.FOCUS_DOWN);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			scrollView.pageScroll(ScrollView.FOCUS_UP);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
